package net.mfjassociates.tools;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;

import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;

//VS4E -- DO NOT REMOVE THIS LINE!
public class PromptLogin extends JDialog {

	private static final long serialVersionUID = 1L;
	private String returnUsername=null;
	private String returnPassword=null;
	private JTextField username;
	private JPasswordField password;
	private JLabel jLabel1;
	private JLabel jLabel0;
	private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";
	public PromptLogin() {
		initComponents();
	}

	private void initComponents() {
		setLayout(new GroupLayout());
		add(getUsernameField(), new Constraints(new Leading(137, 117, 10, 10), new Leading(22, 10, 10)));
		add(getPasswordField(), new Constraints(new Leading(138, 116, 12, 12), new Leading(48, 10, 10)));
		add(getJLabel1(), new Constraints(new Leading(19, 107, 12, 12), new Leading(48, 12, 12)));
		add(getJLabel0(), new Constraints(new Leading(17, 110, 12, 12), new Leading(22, 12, 12)));
		setSize(284, 91);
		setModal(true);
	}

	private JLabel getJLabel0() {
		if (jLabel0 == null) {
			jLabel0 = new JLabel();
			jLabel0.setText("Username");
		}
		return jLabel0;
	}

	private JLabel getJLabel1() {
		if (jLabel1 == null) {
			jLabel1 = new JLabel();
			jLabel1.setText("Password");
		}
		return jLabel1;
	}

	private JTextField getPasswordField() {
		if (password == null) {
			password = new JPasswordField();
			password.addKeyListener(new KeyAdapter() {
	
				public void keyTyped(KeyEvent event) {
					passwordKeyKeyTyped(event);
				}
			});
		}
		return password;
	}

	private JTextField getUsernameField() {
		if (username == null) {
			username = new JTextField();
			username.addKeyListener(new KeyAdapter() {
	
				public void keyTyped(KeyEvent event) {
					usernameKeyKeyTyped(event);
				}
			});
		}
		return username;
	}

	private static void installLnF() {
		try {
			String lnfClassname = PREFERRED_LOOK_AND_FEEL;
			if (lnfClassname == null)
				lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
			UIManager.setLookAndFeel(lnfClassname);
		} catch (Exception e) {
			System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
					+ " on this platform:" + e.getMessage());
		}
	}

	public String getUsername() {
		return username.getText();
	}
	public void setUsername(String username) {
		this.username.setText(username);
	}

	public String getPassword() {
		return password.getText();
	}
	public void setPassword(String password) {
		this.password.setText(password);
	}
	
	/**
	 * This is an example of how to use the class
	 */
	public static void main(String[] args) {
		PromptLogin frame = new PromptLogin();
		setupGUI(frame);
		System.out.println(frame.returnUsername);
		System.out.println(frame.returnPassword);
	}

	public static void setupGUI(PromptLogin frame) {
		installLnF();
		frame.setDefaultCloseOperation(PromptLogin.HIDE_ON_CLOSE);
		frame.setTitle("TestFrame");
		frame.getContentPane().setPreferredSize(frame.getSize());
		frame.pack();
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}

	private void usernameKeyKeyTyped(KeyEvent event) {
		if (event.getKeyChar()=='\n') {
			password.requestFocusInWindow();
		}
	}

	private void passwordKeyKeyTyped(KeyEvent event) {
		if (event.getKeyChar()=='\n') {
			returnUsername=username.getText();
			returnPassword=password.getText();
			this.dispose();
		}
	}

}
